import {computed, ref} from 'vue'
export default function useEditTodo(todosRef){
     const editingTodoRef = ref(null) //当前正在修改的那个todo
     let originTitle = null  //缓存之前的title值
     const editTodo = (todo)=>{
        originTitle = todo.title
        editingTodoRef.value = todo
     }
     //完成修改
     const doneEditTodo = ()=>{
        editingTodoRef.value = null
     }
     //按esc取消修改
     const cancelEditTodo = (todo)=>{
        editingTodoRef.value = null
        todo.title = originTitle
     }
     //全部完成或者取消
     const allDoneRef = computed({
        get(){
            return todosRef.value.filter(it=>!it.completed).length===0
        },
        // set(checked){
        //     todosRef.value.forEach((todo)=>{
        //         todo.completed = checked
        //     })
        // }
     })
     function setAllChecked(checked){
            todosRef.value.forEach((todo)=>{
                todo.completed = checked
            })
     }
     return {
        editingTodoRef,
        editTodo,
        doneEditTodo,
        cancelEditTodo,
        allDoneRef,
        setAllChecked
     }

     //vue3中计算属性中set已经不能使用 
     /**
      * @deprecated 过期函数 以后可能会移除
      */
     function abc(){}
     abc()
}